package main

import (
	"net/http"
	"github.com/julienschmidt/httprouter"
	"io"
	"gitee.com/Cookie_XiaoD/video_server/api/dbops"
	"strconv"
	"encoding/json"
	"log"
	"time"
	"gitee.com/Cookie_XiaoD/video_server/utils"
	"io/ioutil"
	"gitee.com/Cookie_XiaoD/video_server/api/defs"
	"gitee.com/Cookie_XiaoD/video_server/api/session"
)


//用户相关
func CreateUser(w http.ResponseWriter,r *http.Request, param httprouter.Params){

	log.Println("createuser->收到请求")
	res,_:=ioutil.ReadAll(r.Body)
	ubody:=&defs.UserCredential{}

	if err:=json.Unmarshal(res,ubody);err!=nil{
		sendErrorResponse(w,defs.ErrorRequestBodyParseFailed)
		return
	}
	log.Printf("createuser->收到请求:%s",ubody.UserName)

	_,err:=dbops.AddUserCredential(ubody.UserName,ubody.Pwd)
	if err!=nil{//添加用户失败

		log.Println("添加用户失败"+err.Error())
		sendErrorResponse(w,defs.ErrorDbErr)
		return
	}

	//添加session
	//todo:根本不保证完整性，纯瞎写...
	sid,err:= session.GenerateNewSessionId(ubody.UserName)
	if err!=nil{
		log.Println("创建sessionid失败："+err.Error())
		sendErrorResponse(w,defs.ErrorInternalServiceErr)
		return
	}

	su:=&defs.SignedUp{
		Success:true,
		SessionId:sid,
	}

	if suJsonBytes,err:=json.Marshal(su);err!=nil {
		sendErrorResponse(w,defs.ErrorInternalServiceErr)
		return
	}else{
		sendNormalResponse(w,201,string(suJsonBytes))
	}


	//var(
	//	success bool
	//	err error
	//)
	//if success,err=dbops.AddUserCredential("戴天辰","123");err!=nil{
	//	io.WriteString(w,err.Error())
	//	return
	//}
	//io.WriteString(w,strconv.FormatBool(success))
}

func Login(w http.ResponseWriter,r *http.Request, param httprouter.Params){
	uname:=param.ByName("user_name")
	var(
		pwd string
		err error
	)
	if pwd,err=dbops.GetUserCredential(uname);err!=nil{
		io.WriteString(w,err.Error())
		return
	}
	io.WriteString(w,pwd)
}

func GetUserInfo(w http.ResponseWriter, r *http.Request, param httprouter.Params) {
	uidStr:=param.ByName("uid")

	log.Println("获取用户信息："+uidStr)
	uid,err:=strconv.Atoi(uidStr)
	if err!=nil{
		io.WriteString(w,err.Error())
		return
	}
	log.Println("准备获取用户信息")
	userInfo,err:=dbops.GetUserInfo(uid)
	if err!=nil{
		io.WriteString(w,err.Error())
		return
	}
	jsonByte,err:=json.Marshal(userInfo)
	if err!=nil{
		io.WriteString(w,err.Error())
		return
	}
	io.WriteString(w,string(jsonByte))
}




//评论相关
func ListComment(w http.ResponseWriter, r *http.Request, params httprouter.Params) {

	err:=r.ParseForm()
	if err!=nil{
		io.WriteString(w,err.Error())
		return
	}
	vid:=params.ByName("vid")

	from:=r.Form.Get("from")
	to:=r.Form.Get("to")

	fromTime,err:=time.Parse(utils.TimeTemplate1,from)
	if err!=nil{
		io.WriteString(w,err.Error())
		return
	}
	toTime,err:=time.Parse(utils.TimeTemplate1,to)
	if err!=nil{
		io.WriteString(w,err.Error())
		return
	}
	log.Printf("准备查询评论,视频ID：%s 开始时间:%v 结束时间:%v",vid,from,to)
	results,err:=dbops.ListComments(vid,fromTime,toTime)
	if err!=nil{
		io.WriteString(w,err.Error())
		return
	}
	jsonBytes,err:=json.Marshal(results)
	io.WriteString(w,string(jsonBytes))
}

////视频相关
//func AddVideo(w http.ResponseWriter, r http.Request, param httprouter.Params) {
//
//}
